<?php
/**
* @package Scribble
*/
use Buan\Config;
use Buan\Core;
use Buan\UrlCommand;
use Buan\View;
$urlRoot = Config::get('ext.Scribble.urlRoot');

// Get the session user queue and reverse for display purposes
$userQueue = ScribbleSession::getUserQueue();
$userQueue = array_reverse($userQueue);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<title><?php $this->html->e(Config::get('ext.Scribble.appName')); ?></title>

	<?php 
	/* External stylesheets */
	echo $this->getView()->getStylesheetsView()->renderAfter($this->getView()->getSlot('systemlog'), $this->getView());
	?>
</head>
<body>
<div id="body">
	<?php if(ScribbleSession::isOccupied() && ScribbleAuth::authorize(ScribbleSession::getUser(), 'scribble-admin')): ?>
		<div id="wrap-head">
			<div id="header" class="float-wrapper">
				<h1 id="app-name"><span><?php $this->html->e(Config::get('ext.Scribble.appName')); ?></span></h1>
				<div id="nav-header">
					[t:<?php $this->html->e(($site = SiteManager::getCurrentSite())===NULL ? 'UNKNOWN SITE' : $site->prefix); ?>:LOGGED_INTO_%s_AS]
					<?php $sep = ''; $uqsize = count($userQueue); foreach($userQueue as $k=>$qu): ?>
						<?php if($k<$uqsize-1): ?>
							<?php echo $sep; ?> <strong><a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'auth', 'user', $qu->id, array('loginas'=>NULL))); ?>"><?php $this->html->e($qu->displayname); ?></a></strong>
						<?php else: ?>
							<?php echo $sep; ?> <strong><?php $this->html->e($qu->displayname); ?></strong>
						<?php endif; ?>
					<?php $sep = '&raquo;'; endforeach; ?>
					- <a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'logout')); ?>">Logout</a>

					| <a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'help')); ?>">Help</a>
					| [t:Language]: <b><?php echo ScribbleSession::getUser()->getPreferences()->get('adminLanguage'); ?></b> (<a href="<?php $this->html->eUrl(UrlCommand::createUrl('scribble', 'preferences')); ?>#admin-language">change</a>)
					| <b><a href="<?php $this->html->eUrl(UrlCommand::createUrl('index')); ?>">View Site</a></b>
				</div>
			</div>
		</div>

		<div id="wrap-nav-main">
			<div id="nav-main">
				<ul>
					<?php
					/* Include all custom admin menus */
					foreach(Config::get('ext.Scribble.adminMenuTemplates') as $amt) {
						/* Buffer manipulation is due to browser quirks rendering inline-block */
						ob_start();
						include($amt);
						$x = preg_replace("/<\/li>[^<]*<li/ims", "</li><li", ob_get_contents());
						ob_end_clean();
						echo $x;
					}
					?>
				</ul>
			</div>
		</div>
	<?php endif; ?>
	
	<?php $sws = $this->getView()->getSlot('secondary')->render(); ?>
	<div id="wrap-workspace">
		<div id="workspace" class="float-wrapper<?php echo $sws=='' ? '' : ' dual-workspace'; ?>">
			<div id="primary-workspace">
				<div>
					<?php echo $this->getView()->getSlot('systemlog')->renderAfter($this->getView()); ?>
				</div>
		
				<div id="slot-action">
					<?php echo $this->getView()->getSlot('action')->render(); ?>
				</div>
			</div>
	
			<?php if($sws!=''): ?>
			<div id="secondary-workspace">
				<div id="slot-secondary">
					<?php echo $sws; ?>
				</div>
			</div>
			<?php endif; ?>
		</div>
	</div>

	<div id="wrap-footer">
		<div id="footer">
			<p><a href="http://www.scribblecms.co.uk/" rel="external">Scribble <?php $this->html->e("v".Config::get('ext.Scribble.version.string')); ?></a></p>
		</div>
	</div>
</div>

<!-- Help dialog wrapper -->
<div id="scribble-help-dialog" title="Help">
</div>

<?php
/* External Javascripts */
include_once(Config::get('core.dir.resources').'/js/Buan.js.php');
echo $this->getView()->getJavascriptsView()->render();
?>

<?php /* Make a few Scribble config vars available to javascript */ ?>
<script type="text/javascript">
Buan.Config.set('ext.Scribble.urlRoot', '<?php $this->html->e(Config::get('ext.Scribble.urlRoot')); ?>');
Buan.Config.set('ext.Scribble.urlSuffix', '<?php $this->html->e(Config::get('ext.Scribble.urlSuffix')); ?>');
</script>
</body>
</html>